package logic

import (
	"google.golang.org/protobuf/proto"
	"svvenus/vgame/model"
	"svvenus/znet/zpb"
	"svvenus/znet/ztcp"
	"time"
)

// Player
// @Description: 玩家
type Player struct {
	model.PlayerDoc

	ConnID            uint32              `bson:"-"`          //连接id
	Online            bool                `bson:"-"`          //是否在线
	TimeDisconnection time.Time           `bson:"-"`          //断线时间
	TimeSave          time.Time           `bson:"-"`          //上次存盘时间
	Conn              *ztcp.TcpConnection `json:"-" bson:"-"` //连接

	Mails  map[string]*Mail  `bson:"-"` //邮件列表
	Equips map[string]*Equip `bson:"-"` //装备列表
}

// SetConn 玩家绑定连接
// @receiver obj
// @param conn
func (player *Player) SetConn(conn *ztcp.TcpConnection) {
	player.Conn = conn
	player.ConnID = conn.ConnID
	conn.BindObj = player
}

// UnsetConn 解除绑定
// @receiver obj
func (player *Player) UnsetConn() {
	if player.Conn != nil {
		player.Conn.BindObj = nil
	}
	player.ConnID = 0
	player.Conn = nil
}

func (player *Player) SendId(msgId zpb.EPid) {
	if player.Conn != nil {
		player.Conn.SendId(msgId)
	}
}
func (player *Player) SendCode(msgId zpb.EPid, code uint16) {
	if player.Conn != nil {
		player.Conn.SendCode(msgId, code)
	}
}
func (player *Player) Send(msgId zpb.EPid, code uint16, msg proto.Message) {
	if player.Conn != nil {
		player.Conn.Send(msgId, code, msg)
	}
}
